var id=url("?recordType")
var subType = url("?subType")
var patientId=url("?patientId")
var recordTime=url("?recordTime")
var type;
$('.checkin_recordtime').text(recordTime)
if(subType!=null){
	subType=url("?subType")
}else{
	subType=0
}
if(id==1){
	$('.checkin_templat_title span').text('今日血糖')
	getSugarTimeTypeByHour()
	Sugar()
}else if(id==2){
	$('.checkin_upload_top span').text('血压打卡')
	$('.checkin_templat_title span').text('今日血压')
	$('.checkin_bottom').hide()
	$('.checkin_pressure_bottom').removeClass('hide')
	$('.main').height('auto')
	BloodPressure()
}else if(id==3){
	$('.checkin_upload_top span').text('运动打卡')
	$('.checkin_templat_title span').text('今日运动')
	SportType()
	$('.checkin_sport_top_ul li').each(function(i){
		$(this).click(function(){
			$('.checkin_sport_top_ul li').eq(i).addClass('checkin_sport_top_li_active').siblings().removeClass('checkin_sport_top_li_active')
			SportCategory($(this).attr('data-value'))
		})
	})
	Sport()
}else if(id==4){
	$('.checkin_upload_top span').text('用药打卡')
	$('.checkin_templat_title span').text('今日用药提示')
	Pill()
	GetTips(patientId,id)
}else if(id==5){
	$('.checkin_upload_top span').text('饮食打卡')
	$('.checkin_templat_title span').text('今日饮食提示')
	Diet()
	GetTips(patientId,id)
}else if(id==6){
	$('.checkin_upload_top span').text('体重打卡')
	$('.checkin_templat_title span').text('今日体重')
	Weight()
}else if(id==7){
	$('.checkin_upload_top span').text('腰围打卡')
	$('.checkin_templat_title span').text('今日腰围')
	Waist()
}else if(id==8){
	$('.checkin_upload_top span').text('尿酮打卡')
	$('.checkin_templat_title span').text('今日尿酮')
	$('.checkin_ketone_ul li').each(function(i){
		$(this).click(function(){
			$('.checkin_ketone_ul li').eq(i).toggleClass('on').siblings().removeClass('on')
		})
	})
	Ketone()
}else if(id==9){
	$('.checkin_upload_top span').text('胰岛素打卡')
	$('.checkin_templat_title span').text('今日胰岛素提示')
	Ins()
	GetTips(patientId,id)
}else if(id=10){
	$('.checkin_upload_top span').text('睡眠打卡')
	$('.checkin_upload_default').hide()
	$('.sleep_notice').show()
	$('.checkin_templat_title span').text('每周睡眠')
	Sleep()
}



//点击跳转小贴士页面
$('.checkin_tip').click(function(){
	window.location.href = './checkinTip.html?type='+id
})



//添加数据ajax
function AddRecord(param){
	$.ajax({
		type:AJAXTYPE,
		url:AJAXURL+"/record/common/add",
		data:JSON.stringify(param),
		dataType: AJAXDATATYPE,
		contentType:AJAXCONTENTTYPE,
		beforeSend: function(){
        },
        success:function(data){
        		console.log(data)
        		if(data.success==true){
        			syalert.syopen('alert3')
        			$('.sy-content').text('您已成功完成本次打卡！')
        		}
        		
        },
        error:function(){
        		console.log('error')
        }
	});
}

//获取运动类型
function SportType(){
	$.ajax({
		type:"GET",
		url:AJAXURL+"/sys/dict/getDictItems/sport_type",
		dataType: AJAXDATATYPE,
		contentType:AJAXCONTENTTYPE,
		beforeSend: function(){
        },
        success:function(data){
        		console.log(data.result)
        		$('.checkin_sport_top_ul li').each(function(i){
        			$(this).text(data.result[i].text).attr('data-value',data.result[i].value)
        		})
        		SportCategory($('.checkin_sport_top_li_active').attr('data-value'))
        },
        error:function(){
        		console.log('error')
        }
	});
}
//获取运动分类
function SportCategory(value){
	$.ajax({
		type:"GET",
		url:AJAXURL+"/sys/dict/getDictItems/"+value,
		dataType: AJAXDATATYPE,
		contentType:AJAXCONTENTTYPE,
		beforeSend: function(){
        },
        success:function(data){
        		console.log(data.result)
        		$('.checkin_sport_category').find('li').remove()
        		$.each(data.result,function(i,v){
        			var li = '<li data-value='+data.result[i].value+' >'+data.result[i].text+'</li>'
        			$('.checkin_sport_category').append(li)
        		})
			$('.checkin_sport_category li').click(function(){
				$(this).toggleClass('on')
			})
        },
        error:function(){
        		console.log('error')
        }
	});
}
//获取提示
function GetTips(pid,type){
	$.ajax({
		type:'GET',
		url:AJAXURL+"/107/01/1070109?patientId="+pid+'&type='+type,
//		data:JSON.stringify(param),
		dataType: AJAXDATATYPE,
		contentType:AJAXCONTENTTYPE,
		beforeSend: function(){
        },
        success:function(data){
        		console.log(data)
        		if(data.success==true){
        			if(data.result.suggestContent){
        				$('.checkin_tips').text(data.result.suggestContent)
        			}else{
        				var html = ['<img src="img/blank_pic.png" style="width:50%"/>',
        							'<div>请记得遵从医嘱哦</div>']
        				$('.checkin_tips').html(html).addClass('text-center')
        				
        			}

        		}
        		
        },
        error:function(){
        		console.log('error')
        }
	});
}


//血糖打卡
function Sugar(){
	$('.checkin_sugar').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		console.log('sugar',imgList,image)
		if(image==''&&$('.sugar_ruler_value').text()=='0.0'){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传血糖图片或输入血糖数值')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image||'',
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
				"sugarValue": $('.sugar_ruler_value').text(),
			})
		}
	})	
}
//血压打卡
function BloodPressure(){
	$('.checkin_pressure').removeClass('hide')
	var image = ''
	$('.checkin_pressure_bottom').click(function(){
		console.log('2')
		image = imgList.join(',')
		console.log('pressure',imgList,image)
		if(image==''&&$('.high_pressure').text()=='0'&&$('.low_pressure').text()=='0'){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传血压图片或输入血压数值')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image||"",
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
				"highPressure": $('.high_pressure').text(),
				"lowPressure": $('.low_pressure').text(),
				"heartPulse": $('.heart_rate').text(),
			})
		}
	})	
}
//运动打卡    力量运动参数 微信步数获取  TODO
function Sport(){
	$('.checkin_sport').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		var sportType = $('.checkin_sport_top_ul').find('.checkin_sport_top_li_active').attr('data-type')
		var sportTypeName = $('.checkin_sport_top_ul').find('.checkin_sport_top_li_active').text()
		var sportcategory = []
		$('.checkin_sport_category li').each(function(i){
			if($('.checkin_sport_category li').eq(i).hasClass('on')){
				sportcategory.push($('.checkin_sport_category li').eq(i).text())
			}
		})
//		if(image==''){
//			syalert.syopen('alert3')
//      		$('.sy-content').text('请上传运动图片或选择运动类型')
//		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image||"",
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
//				"sportValue": ,	//运动步数
				"sportType": sportType,   //运动类型 1慢走 2快走
				"sportTypeName":sportTypeName,
				"sportName":sportcategory.join(',')
			})
//		}
	})	
}
//用药打卡     用药提示  TODO
function Pill(){
	$('.checkin_pill').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		console.log('pill',imgList,image)
		if(image==''){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传用药图片')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image,
				"recordType": id,
				"subType":subType,
				"recordTime": $('.checkin_date_span').text() + ':00',
			})
		}
	})	
}
//饮食打卡     饮食提示  TODO
function Diet(){
	$('.checkin_diet').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		console.log('diet',imgList,image)
		if(image==''){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传饮食图片')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image,
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
			})
		}
	})	
}
//体重打卡
function Weight(){
	$('.checkin_weight').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		console.log('weight',imgList,image)
		if(image==''&&$('.weight_ruler_value').text()=='0.0'){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传体重图片或输入体重数值')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image||"",
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
				"weight": $('.weight_ruler_value').text(),
			
			})
		}
	})	
}
//腰围打卡
function Waist(){
	$('.checkin_waist').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		console.log('waist',imgList,image)
		if(image==''&&$('.waist_ruler_value').text()=='0.0'){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传腰围图片或输入腰围数值')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image||"",
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
				"waist": Math.floor($('.waist_ruler_value').text()).toString(),
			
			})
		}
	})	
}
//尿酮打卡
function Ketone(){
	$('.checkin_ketone').removeClass('hide')
	$('.checkin_bottom').hide()
	$('.main').height('auto')
	$('.checkin_keton_method').removeClass('hide')
//	$('.img_link,.checkin_keton_method').removeClass('hide')
	var image = ''
	$('.checkin_keton_btn').click(function(){
		var instype = ''
		instype = $('.checkin_ketone_ul li.on').attr('data-type') //获取尿酮选项 选填
		console.log('ins',instype)
		image = imgList.join(',')
		console.log('ins',imgList,image)
		if(image==''){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传尿酮图片')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": image,
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
				"ketone":instype
			})
		}
	})	
}
//胰岛素打卡     胰岛素提示  TODO
function Ins(){
	$('.checkin_ins').removeClass('hide')
	var image = ''
	$('.checkin_bottom').click(function(){
		image = imgList.join(',')
		console.log('sugar',imgList,image)
		if(image==''){
			syalert.syopen('alert3')
        		$('.sy-content').text('请上传胰岛素图片')
		}else{
			AddRecord({
				"patientId": patientId || 0,
				"images": "",
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,
				"subType":subType,
			})
		}
	})	
}
//睡眠打卡
function Sleep(){
	$('.checkin_sleep').removeClass('hide')
	$('.checkin_bottom').click(function(){
		console.log('sleep')
			AddRecord({
				"patientId": patientId || 0,
				"recordTime": $('.checkin_date_span').text() + ':00',
				"recordType": id,   //10
				"subType":subType,
				"sleep": $('.checkin_sleep_value').text(), //0-7
			})
	})	
}


//图片上传
function postData(data){
    $.ajax({
	url: AJAXURL+'/sys/common/upload',
	type: 'post',
	data: data,
	contentType: false,
	processData: false,
	beforeSend: function(){
		Dreamer.loading("图片上传中");
    },
	success: function(res) {
		console.log('upload', res);
		
		if(res.success==true){
			Dreamer.destroyAll();
			if($('.main').height()<$(document.body).height()){
//				$('.main').height('94%')
			}
			imgList.push(res.message)
			
		}
	},
        error:function(){
        		console.log('error')
        }
	})
}
